﻿using System;
using System.Collections.Generic;
using System.Linq;
using Sumen.Model.Model;

namespace Sumen.Business.Presenter.Sys
{
    public class UserPresenter
    {
        public int Update(object pObjectValue, object pId)
        {
            using (sumenEntitiesSystem objEntities = new sumenEntitiesSystem())
            {
                user newUser = pObjectValue as user;

                string user = pId + string.Empty;
                user oldUser = objEntities.users.FirstOrDefault(p => p.User_Username == user);
                oldUser.User_Username = newUser.User_Username;
                if (newUser.User_Password != "")
                {
                    newUser.User_Password = ClsPublic.EncryptSHA512(newUser.User_Password);
                    oldUser.User_Password = newUser.User_Password;
                    oldUser.User_LastChangePassword = DateTime.Today;
                }
                oldUser.User_FullName = newUser.User_FullName;
                oldUser.User_Description = newUser.User_Description;
                //oldUser.User_Role_ID = newUser.User_Role_ID;
                oldUser.User_Phone = newUser.User_Phone;
                oldUser.User_Address = newUser.User_Address;
                oldUser.User_Email = newUser.User_Email;
                oldUser.User_Contact = newUser.User_Contact;
                oldUser.User_Actived = newUser.User_Actived;
                return objEntities.SaveChanges();
            }
        }

        public int Add(object pObjectValue)
        {
            using (sumenEntitiesSystem objEntities = new sumenEntitiesSystem())
            {
                user newUser = pObjectValue as user;
                newUser.User_Password = ClsPublic.EncryptSHA512(newUser.User_Password);
                objEntities.users.Add(newUser);
                return objEntities.SaveChanges();
            }
        }

        public int Delete(object pObjectValue)
        {
            using (sumenEntitiesSystem objEntities = new sumenEntitiesSystem())
            {
                string user = pObjectValue + string.Empty;
                objEntities.users.Remove(
                    objEntities.users.FirstOrDefault(p => p.User_Username == user));
                return objEntities.SaveChanges();
            }
        }

        public ICollection<user> GetAll()
        {
            using (sumenEntitiesSystem objEntities = new sumenEntitiesSystem())
            {
                return objEntities.users.ToList();
            }
        }

        public user GetById(object pId)
        {
            using (sumenEntitiesSystem objEntities = new sumenEntitiesSystem())
            {
                string user = pId + string.Empty;
                return objEntities.users.FirstOrDefault(p => p.User_Username == user);
            }
        }


    }
}
